package com.gome.gop;

import com.gome.gop.mybatis.DynamicDataSources;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;

/**
 * 应用主入口
 * 
 * @file WebApplication.java
 * @tags
 * @copyright gome.com.cn @ copyright
 * @date 2018年2月6日 下午4:15:22
 * @author jiangdabin
 */
@EnableDiscoveryClient // 用于启动服务发现功能
@EnableFeignClients // 用于启动Fegin功能
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.gome.gop.data.mapper")
@Configuration
@DynamicDataSources("com.gome.gop.data.mapper")
public class WebApplication extends SpringBootServletInitializer {
    private static Logger logger = LoggerFactory.getLogger(WebApplication.class);
    
    public static void main(String[] args) {
        logger.debug("启动应用");
        new SpringApplicationBuilder(WebApplication.class)
            .properties("spring.config.name:application,mybatis,redis,restutils,pddutils,taskexecutor,newFileUtils", "spring.config.location:classpath*:/")
            .build()
            .run(args);
    }
    
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        logger.debug("启动应用");
        return application.sources(WebApplication.class)
            .properties("spring.config.name:application,mybatis,redis,restutils,pddutils,taskexecutor,newFileUtils", "spring.config.location:classpath*:/");
    }
    
    /**
     * 配置上传文件大小的配置
     * 
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 单个数据大小
        factory.setMaxFileSize("102400KB");
        /// 总上传数据大小
        factory.setMaxRequestSize("102400KB");
        return factory.createMultipartConfig();
    }
    
}
